import 'package:flutter/material.dart';
import 'dart:ui' as ui;
import 'dart:math' as math;
import 'dart:ui' as ui;
import 'package:flutter/services.dart';


class DrawEmojiUtil {
  static Future<Uint8List?> drawEmoji(int width, int height, int textHeight, String str,double rotate) async {
    var recorder = ui.PictureRecorder();
    var canvas = Canvas(recorder);

    TextPainter textPainter = TextPainter(
        text: TextSpan(text: str, style: const TextStyle(fontSize: 100.0, color: Colors.white)),
        textDirection: TextDirection.ltr);

    textPainter.layout();
    print("textPainter.size:${textPainter.size}");

    // 绘制文字
    textPainter.paint(canvas, const Offset(0.0, 0.0));

    // 结束录制并获取Picture
    var picture = recorder.endRecording();
    var image = await picture.toImage(textPainter.size.width.toInt(), textPainter.size.height.toInt());

    var size = Size(textHeight.toDouble(), textHeight.toDouble());
    //压缩
    image = await resizeImage(image, size);

    //旋转
    image = await rotateImage(image, rotate);


    double startX = 0;
    double startY = (height - size.height) / 2;

    //调整尺寸,居中处理
    recorder = ui.PictureRecorder();
    canvas = Canvas(recorder);
    canvas.drawImage(image, Offset(startX, startY), Paint());
    picture = recorder.endRecording();
    image = await picture.toImage(size.width.toInt(), height);

    // 将 Image 对象转换为字节数据（PNG 格式）
    final ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);

    return byteData?.buffer.asUint8List();
  }

  static double degreeToRadian(double degrees) {
    return degrees * (math.pi / 180);
  }

  static Future<ui.Image> rotateImage(ui.Image image, double degrees) async {
    if(degrees == 0){
      return image;
    }
    degrees = degreeToRadian(degrees);
    final recorder = ui.PictureRecorder();
    final canvas = Canvas(recorder);

    // 将角度转换为弧度
    final centerX = image.width / 2.0;
    final centerY = image.height / 2.0;

    // 移动画布原点到图像中心
    canvas.translate(centerX, centerY);

    // 旋转画布
    canvas.rotate(degrees);

    // 将图像绘制在画布上（注意调整位置使其以中心为基准）
    canvas.drawImage(image, Offset(-centerX, -centerY), Paint());

    // 完成录制并生成新的图像
    final picture = recorder.endRecording();
    final rotatedImage = await picture.toImage(image.width, image.height);
    return rotatedImage;
  }

  static Future<ui.Image> drawText(int width, int height, int textHeight, String str) async {
    var recorder = ui.PictureRecorder();
    var canvas = Canvas(recorder);

    TextPainter textPainter = TextPainter(
        text: TextSpan(text: str, style: TextStyle(fontSize: textHeight.toDouble(), color: Colors.white)),
        textDirection: TextDirection.ltr);

    textPainter.layout();
    print("textPainter.size:${textPainter.size}");

    // 绘制文字
    textPainter.paint(canvas, const Offset(0.0, 0.0));

    // 结束录制并获取Picture
    var picture = recorder.endRecording();

    //var trueWidth = textPainter.width + (width - textPainter.width % width);
    var trueWidth = textPainter.width;

    var image = await picture.toImage(trueWidth.toInt(), height);

    //GifDecoder.image2Png(image, "tmp123.png");
    return image;
  }


  static Future<ui.Image> resizeImage(ui.Image image, Size size) async {
    final recorder = ui.PictureRecorder();
    final canvas = Canvas(recorder);
    final paint = Paint();
    paint.isAntiAlias = false;
    //canvas.drawImage(image, Offset.zero, paint);
    //计算居中后的尺寸和位置

    var s = size.width / image.width;
    if (s * image.height > size.height) {
      s = size.height / image.height;
    }

    var dw = image.width * s;
    var dh = image.height * s;

    var dx = (size.width - dw) / 2;
    var dy = (size.height - dh) / 2;

    canvas.drawImageRect(image, Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble()),
        Rect.fromLTWH(dx, dy, dw, dh), paint);
    final picture = recorder.endRecording();
    return picture.toImage(size.width.toInt(), size.height.toInt());
  }

}
